﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using CodeStudy.WCF.Service;

namespace CodeStudy.NoneUI
{
    class Program
    {
        #region :: Constant Value ::
        /// <summary>
        /// The max number of role.
        /// </summary>
        private const int MaxRoleNumber = 1;

        /// <summary>
        /// The role number of WCF service.
        /// </summary>
        private const int WcfServiceRoleNumber = 1;

        /// <summary>
        /// The command string of stop performance.
        /// </summary>
        public const string StopPerformanceCommand = "stop";
        #endregion

        #region :: Properties ::
        /// <summary>
        /// Gets or sets the current role number.
        /// </summary>
        /// <value>
        /// The current role number.
        /// </value>
        public static int CurrentRoleNumber { get; private set; }

        /// <summary>
        /// Gets the performance context.
        /// </summary>
        public static object PerformanceContext { get; private set; }
        #endregion

        #region :: Main Process ::
        /// <summary>
        /// Mains the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        static void Main(string[] args)
        {
            Console.WriteLine("Please choose a  role for me:");
            Console.WriteLine("1 : WCF Service");
            int number = GetRoleNumber();

            switch (number)
            {
                case WcfServiceRoleNumber:
                    StartWcfService();
                    break;
            }

            Console.WriteLine("Press any key to exit:");
            Console.ReadLine();
        }

        /// <summary>
        /// Gets the role number.
        /// </summary>
        /// <returns>Role Number</returns>
        private static int GetRoleNumber()
        {
            bool correctInput = false;
            int number = -1;

            while (!correctInput)
            {
                Console.WriteLine("Please input the role number:");
                string input = Console.ReadLine();

                try
                {
                    number = int.Parse(input);
                    correctInput = number > 0 && number <= MaxRoleNumber;
                }
                catch
                {
                    correctInput = false;
                }
            }
            return number;
        }

        /// <summary>
        /// Stops the performance.
        /// </summary>
        private static void StopPerformance()
        {
            bool run = true;
            while (run)
            {
                Console.WriteLine("Input \"Stop\" to stop current performance:");
                string input = Console.ReadLine();
                if (input.Trim().ToLower() == StopPerformanceCommand.Trim().ToLower())
                    run = false;
            }
        }
        #endregion

        #region :: WCF Service ::
        /// <summary>
        /// Starts the WCF service.
        /// </summary>
        private static void StartWcfService()
        {
            using (ServiceHost host = new ServiceHost(typeof(MyService)))
            {

                host.Open();
                StopPerformance();
                host.Close();
            }
        }
        #endregion
    }
}
